package com.wlb365.admin.common.utils;

import com.wlb365.admin.common.exception.AdminException;
import com.wlb365.admin.properties.AdminProperties;
import com.wlb365.admin.properties.SdkProperties;
import com.wlb365.sdk.client.SdkClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 概述：
 * </p>
 *
 * @author zwh
 * @version 1.0
 * @copyRight 北京微利宝信息技术有限公司  All right reserved.
 * @date 2020/5/22
 */
public class SdkUtils {
    private static Map<String, SdkClient> sdkClientMap;
    private static AdminProperties adminProperties = SpringContext.getBean(AdminProperties.class);

    public static SdkClient getClient() {
        if (sdkClientMap == null) {
            sdkClientMap = new HashMap<>();
        }

        if (adminProperties.getSdk() == null) {
            throw new AdminException("初始化SDK失败！");
        }

        SdkProperties sdkProperties = adminProperties.getSdk();

        if (sdkClientMap.containsKey(sdkProperties.getAppId())) {
            return sdkClientMap.get(sdkProperties.getAppId());
        }

        SdkClient sdkClient = new SdkClient(sdkProperties.getUrl(), sdkProperties.getAppId()
                , sdkProperties.getPrivateKey()
                , sdkProperties.getPublicKey()
        );
        sdkClientMap.put(sdkProperties.getAppId(), sdkClient);
        return sdkClient;
    }
}
